package com.testmanager.testmananger.quantitative.service;

import com.testmanager.testmananger.foundation.util.FlakeIdGenerator;
import com.testmanager.testmananger.quantitative.constants.Sign;
import com.testmanager.testmananger.quantitative.domain.DevelopInfoEntity;
import com.testmanager.testmananger.quantitative.domain.WorkerEntity;
import com.testmanager.testmananger.quantitative.dto.DevelopInfoDto;
import com.testmanager.testmananger.quantitative.dto.TaskInfoDto;
import com.testmanager.testmananger.quantitative.dto.WorkerDto;
import com.testmanager.testmananger.quantitative.mapper.DevelopInfoMapper;
import com.testmanager.testmananger.quantitative.mapper.WorkerMapper;
import com.testmanager.testmananger.quantitative.repository.DevelopInfoRepository;
import com.testmanager.testmananger.quantitative.repository.WorkerRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DevelopInfoService {
    @Autowired
    WorkerRepository workerRepository;

    @Autowired
    DevelopInfoRepository developInfoRepository;
    @Autowired
    TaskInfoService taskInfoService;
    @Autowired
    WorkService workService;
@Autowired
    FlakeIdGenerator flakeIdGenerator ;

    public DevelopInfoDto getDevelopInfoWithTaskInfo(long iterationId){
        DevelopInfoEntity developInfoEntity = developInfoRepository.findByIterationId(iterationId);
        if (developInfoEntity == null) return null;
        List<TaskInfoDto> taskInfoDtos = taskInfoService.getTaskInfoDtos(developInfoEntity.getDevelopNo());
        List<WorkerDto> workerDtos = workerRepository.findWorkerEntitiesByOwnerNo(developInfoEntity.getDevelopNo())
                .stream()
                .map(WorkerMapper::toWorkerDto)
                .collect(Collectors.toList());
        return DevelopInfoMapper.toDevelopInfoDto(developInfoEntity, workerDtos, taskInfoDtos);
    }

    public DevelopInfoDto getDevelopInfoWithoutTaskInfo(long iterationId){
        DevelopInfoEntity developInfoEntity = developInfoRepository.findByIterationId(iterationId);
        if (developInfoEntity == null) return null;
        List<TaskInfoDto> taskInfoDtos = null;

        List<WorkerDto> workerDtos = workerRepository.findWorkerEntitiesByOwnerNo(developInfoEntity.getDevelopNo())
                .stream()
                .filter(Objects::nonNull)
                .map(workEntity-> WorkerMapper.toWorkerDto(workEntity))
                .collect(Collectors.toList())
                ;
        return DevelopInfoMapper.toDevelopInfoDto(developInfoEntity, workerDtos, taskInfoDtos);
    }

    @Transactional
    public DevelopInfoDto saveDevelopInfoWithoutTaskInfo(long iterationId,DevelopInfoDto developInfoDto){
        log.info("保存g开发基础信息：{},迭代id:{}",developInfoDto,iterationId);
        if (developInfoDto.getDevelopNo() ==0) developInfoDto.setDevelopNo(flakeIdGenerator.newId());
        DevelopInfoEntity developInfoEntity = DevelopInfoMapper.toDevelopInfoEntity(developInfoDto, iterationId);
        List<WorkerDto> workerDtos = developInfoDto.getWorkers();
        workService.saveWorkerDtos(workerDtos, developInfoDto.getDevelopNo());

        developInfoRepository.save(developInfoEntity);
        return getDevelopInfoWithTaskInfo(iterationId);
    }

}
